package cn.lastcrdg.testitemdecoration;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.blankj.utilcode.util.SizeUtils;

/**
 * author：liluo
 * e-mail：lastcrazysun@163.com
 * company：cusc
 * date：2018/4/9 16:11
 * description：
 */
public class HotItemDecoration extends RecyclerView.ItemDecoration{

    private int dividerLeft = 160;
    private int dividerTop = 40;
    private int textSize = SizeUtils.sp2px(16);
    private Paint mPaint;
    private Bitmap mBitmap;

    public HotItemDecoration(Context context) {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(4);
        mPaint.setColor(Color.GRAY);

        mBitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.hot);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);

        outRect.left = dividerLeft;
        if (parent.getChildAdapterPosition(view) != 0) {
            outRect.top = dividerTop;
        }
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);

        int count = parent.getChildCount();

        for (int i = 0; i < count; i++) {

            mPaint.setColor(Color.GRAY);
            View view = parent.getChildAt(i);
            int pos = parent.getChildAdapterPosition(view);

            if (pos != 0){
                c.drawLine(0, view.getTop()-dividerTop/2, view.getLeft()+ view.getMeasuredWidth(), view.getTop()-dividerTop/2, mPaint);
            }

            int textX = dividerLeft / 2 - textSize/2 + SizeUtils.dp2px(10);
            int textY = view.getMeasuredHeight() / 2 + view.getTop() + textSize/2;

            mPaint.setTextSize(textSize);
            if (pos < 3){
                mPaint.setColor(Color.RED);
            }
            c.drawText(String.valueOf(pos + 1), textX, textY, mPaint);
        }
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);

        int w = SizeUtils.dp2px(40);
        Rect rect = new Rect(0,0,w,w);

        int count = parent.getChildCount();
        for (int i = 0; i < count; i++) {
            View view = parent.getChildAt(i);
            int pos = parent.getChildAdapterPosition(view);

            if (pos < 3){
                int left = dividerLeft/2 - w/2 + SizeUtils.dp2px(10);
                int top = view.getTop() + view.getPaddingTop();
                int right = left + w;
                int bottom = top + w;
                Rect outRect = new Rect(left, top, right, bottom);
                c.drawBitmap(mBitmap, rect, outRect, mPaint);
            }
        }
    }
}
